class Kinud_Aircraft extends UTAirVehicle;

/* etc */

var float AR_MaxLift, AR_TurnSpeed, AR_Speed, AR_CurrentForward, AR_AddedLiftSpeed, AR_Acceleration;
var Vector CameraLoc;
var float ForwardForce,MaxPitchTorque,MaxYawTorque,MaxRollTorque;
var float MinSpeed,AverageSpeed,MaxSpeed;
var bool bAccelerate,bModerate;
var UDKinectInstance UDKinectInstance;

simulated function ProcessViewRotation(float DeltaTime, out rotator out_ViewRotation, out rotator out_DeltaRot)
{
	local Vector AR_ForceApplication, AR_ForceRotation, AR_ForwardsNormal, AR_UpwardsNormal, AR_SidewardsNormal;
	local Vector AR_ForceRotationVectX, AR_ForceRotationVectY, AR_ForceRotationVectZ;
	local float  ForceRotationVectXsize, ForceRotationVectYsize, ForceRotationVectZsize;

	//out_ViewRotation = RTransform(out_DeltaRot,out_ViewRotation);
	out_ViewRotation = Rotation;


	GetAxes(Rotation, AR_ForwardsNormal, AR_SidewardsNormal, AR_UpwardsNormal);

	// It's probably unnecessary to normalise these:
	AR_ForceRotationVectX = Normal(AR_ForwardsNormal);
	AR_ForceRotationVectY = Normal(AR_SidewardsNormal);
	AR_ForceRotationVectZ = Normal(AR_UpwardsNormal);

	// Pitch
	AR_ForceRotationVectY *= -0.3 * out_DeltaRot.Pitch;
	// Yaw
	AR_ForceRotationVectZ *= 0.3 * out_DeltaRot.Yaw;
	//Roll
	AR_ForceRotationVectX *= 60 * Steering;

	ForceRotationVectYsize = VSize(AR_ForceRotationVectY);
	ForceRotationVectZsize = VSize(AR_ForceRotationVectZ);
	ForceRotationVectXsize = VSize(AR_ForceRotationVectX);

	if(ForceRotationVectYsize>MaxPitchTorque)
		//AR_ForceRotationVectY /= (ForceRotationVectYsize/MaxPitchTorque);
		AR_ForceRotationVectY = Normal(AR_ForceRotationVectY)*MaxPitchTorque;
	if(ForceRotationVectZsize>MaxPitchTorque)
		//AR_ForceRotationVectY /= (ForceRotationVectZsize/MaxYawTorque);
		AR_ForceRotationVectZ = Normal(AR_ForceRotationVectZ)*MaxYawTorque;
	if(ForceRotationVectXsize>MaxPitchTorque)
		//AR_ForceRotationVectY /= (ForceRotationVecXsize/MaxRollTorque);
		AR_ForceRotationVectX = Normal(AR_ForceRotationVectX)*MaxRollTorque;



	AR_ForceRotation = AR_ForceRotationVectX + AR_ForceRotationVectY + AR_ForceRotationVectZ;
	Mesh.AddTorque(AR_ForceRotation);



	out_DeltaRot		 = rot(0,0,0);

}


exec function SpaceButtonDown()
{
	bAccelerate = true;
}

exec function ControlButtonDown()
{
	bModerate = true;
}

exec function SpaceButtonUp()
{
	bAccelerate = false;
}

exec function ControlButtonUp()
{
	bModerate = false;
}
/*
simulated function VehicleCalcCamera(float DeltaTime, int SeatIndex, out vector out_CamLoc, out rotator out_CamRot, out vector CamStart, optional bool bPivotOnly)
{
	local Vector ForwardsNormal, UpwardsNormal, SidewardsNormal;

	if (SeatIndex == 0)
	{
		GetAxes(Rotation, ForwardsNormal, SidewardsNormal, UpwardsNormal);
		//out_CamLoc = Location + CameraLoc >> Rotation;
		out_CamLoc = Location + CameraLoc.X * ForwardsNormal + CameraLoc.Z * UpwardsNormal;
		out_CamRot = Rotation;
		CamStart = out_CamLoc;
	}
	else
	{
		Super.VehicleCalcCamera(DeltaTime, SeatIndex, out_CamLoc, out_CamRot, CamStart, bPivotOnly);
	}

}
*/
simulated function ControlCraft()
{

	local Vector ForwardsNormal, UpwardsNormal, SidewardsNormal,Friction;
	local float Speed;

    GetAxes(Rotation, ForwardsNormal, SidewardsNormal, UpwardsNormal);
    Speed = Velocity dot ForwardsNormal;

    if(bAccelerate)
    {
        if(Speed <= MaxSpeed)
        {
            GetAxes(Rotation, ForwardsNormal, SidewardsNormal, UpwardsNormal);
            Mesh.AddForce(ForwardsNormal * ForwardForce);
        }
    }
    else if(bModerate)
    {
        if(Speed >= MinSpeed)
        {
            GetAxes(Rotation, ForwardsNormal, SidewardsNormal, UpwardsNormal);
            Mesh.AddForce(-ForwardsNormal * ForwardForce);
        }
    }
}

simulated function Tick(float DeltaTime)
{
	ControlCraft();
	Super.Tick(DeltaTime);	    
	if(UDKinectInstance.UDKinectIsSensorReady())
 	{
 		self.clientMessage("Kinect Ready!");
 	}
 	else
 	{
 		self.clientMessage("Kinect not Ready!");
 	}
}

DefaultProperties
{
	AR_MaxLift = 100
	AR_AddedLiftSpeed = 0
	AR_Acceleration = 10

	MaxPitchTorque = 70
	MaxYawTorque = 70
	MaxRollTorque = 70

	MinSpeed = 30
	AverageSpeed = 600
	MaxSpeed = 10000

	CameraLoc = (X=-780,Z=180)
	ForwardForce = 320

    CustomGravityScaling=0.0
    bAccelerate = false
    bModerate = false

	UDKinectInstance = Spawn(class'UDKinectInstance');

}
